package cn.taichoo.server.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.taichoo.server.service.IAppService;
import com.ruoyi.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import cn.taichoo.server.domain.AppEntity;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * App主，基本信息Controller
 *
 * @author Wuzh
 * @date 2025-03-17
 */
@Tag(name = "APP管理" )
@RestController
@RequestMapping("/info" )
public class AppController extends BaseController {

    @Resource
    private IAppService iAppService;

    /**
     * 查询App主，基本信息列表
     */
    @RequiresPermissions("server:info:list" )
    @Operation(summary = "APP列表查询" )
    @GetMapping("/list" )
    public TableDataInfo list(AppEntity appEntity) {
        startPage();
        List<AppEntity> list = iAppService.selectAppEntityList(appEntity);
        return getDataTable(list);
    }

    /**
     * 导出App主，基本信息列表
     */
    @RequiresPermissions("server:info:export" )
    @Log(title = "App主，基本信息", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出App" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, AppEntity appEntity) {
        List<AppEntity> list = iAppService.selectAppEntityList(appEntity);
        ExcelUtil<AppEntity> util = new ExcelUtil<AppEntity>(AppEntity.class);
        util.exportExcel(response, list, "App主，基本信息数据" );
    }

    /**
     * 获取App主，基本信息详细信息
     */
    @RequiresPermissions("server:info:query" )
    @Operation(summary = "获取App主，基本信息详细信息" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return success(iAppService.selectAppEntityById(id));
    }

    @RequiresPermissions("server:info:add" )
    @Operation(summary = "保存App主，基本信息" )
    @Log(title = "App主，基本信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/{id}" )
    public AjaxResult saveSingle(@RequestBody AppEntity appEntity) {
        return iAppService.insert(appEntity);
    }

    /**
     * 修改BOM清单
     */
    @RequiresPermissions("server:info:edit" )
    @Operation(summary = "修改App主，基本信息" )
    @Log(title = "App主，基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppEntity appEntity) {
        return iAppService.updateAppById(appEntity);
    }

    @RequiresPermissions("server:info:remove" )
    @Operation(summary = "删除App主，基本信息" )
    @Log(title = "删除App主，基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}" )
    public AjaxResult remove(@PathVariable Long id) {
        return iAppService.deleteById(id);
    }

    /**
     * 获取App主，通过包名查询APP
     * 提给给Feign
     */
    @InnerAuth
    @Operation(summary = "获取App主，通过包名查询APP，提给给Feign" )
    @PostMapping("getAppByPackage" )
    public AjaxResult getAppByPackage(@RequestBody String packageName) {
        return success(iAppService.getAppByPack(packageName));
    }

}
